const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myfiles:[],
    files: [],
    nums: '', //初始数量
    activeNames: [''],
    show: false,
    shows: false,
    message: '',
    _id: '',
    index:'',
    time: '',
    user_title: '',
    user_url: '',
    message_type: '评论',
    confirm: '',
    openId:'',
    target:'',
      //画廊功能
      imgUrls: [],
      img_view: false,
    img_view_index: '',
  },
  edit() {
    wx.navigateTo({
      url: '/pages/tabbar/square_edit/square_edit',
    })
  },
  onChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var newDateTime = Date.parse(new Date())
    this.setData({
      time: newDateTime
    })

    
    var openId = wx.getStorageSync('openId')
    // console.log(openId)
    // 查询数据库中是否有用户数据
    let res = db.collection("user").doc(openId).get()
      .then(res => {
        this.setData({
          user_url: res.data.user_url,
          user_title: res.data.user_title,
          openId:openId
        })
      })


    // 云函数获取数据
    wx.cloud.callFunction({
        // 云函数名称
        name: 'getsquare'
      })
      .then(res => {
        console.log('全部论坛获取', res)
        this.setData({
          files: res.result.data
        })
      })
      .catch(err => {
        console.log('全部论坛获取失败', err)
      })


 
  },

  // 点赞处理事件
  like(e) {
    // console.log(e)
    let id = this.data.files[e.target.id]._id
    console.log(id)
    db.collection("square").doc(id).update({
      data: {
        like: this.data.files[e.target.id].like + 1
      }
    })
    console.log('跟新成功')
    this.setData({
      ['files[' + e.target.id + '].like']: this.data.files[e.target.id].like + 1,
    })
  },


  // 评论处理事件
  pinglun(e) {
    console.log(e)
    this.setData({
      message_type: '评论',
      confirm: 'confirm',
      show: true,
      _id: e.target.id
    });
  },
  cancel() {
    this.setData({
      show: false
    });
    this.setData({
      message: ''
    });
  },
  pl(event) {
    this.setData({
      message: event.detail
    });
  },



  // 点击确认按钮
  confirm(e) {
    let _id = this.data._id
    let message = this.data.message
    var newDateTime = Date.parse(new Date())
    let user_title = this.data.user_title
    let user_url = this.data.user_url
    let openId = this.data.openId
    db.collection("user").doc(openId).get()
      .then(res => {
        if (res.data.user_vip !== '') {
          var vip = true
        }
        const _ = db.command
        db.collection('square').doc(_id).update({
          data: {
            message: _.push({
              user_url:user_url,
              user_title: user_title,
              user_vip: vip,
              message: message,
              time: newDateTime,
              _id: _id,
              type: 0

            })
          }
        });
      })
    this.setData({
      show: false,
      message: ''
    });


    setTimeout(() => {
      //延迟后进行的操作
      this.onLoad();

    }, 2000) //delayTime是延迟时间，以毫秒为单位，1000ms=1s

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad()
  },



  // 一级品类的huifu
  onehuifu(e) {
    var n = e.currentTarget.id.split("+");
    console.log(n)
    this.setData({
      confirm: 'confirms',
      message_type: '回复',
      show: true,
      index: n[0],
      _id: n[1],
      target:n[2]
    })
  },



  // 点击确认之后上传【回复评论】
  confirms(e) {
    let _id = this.data._id
    let message = this.data.message
    var newDateTime = Date.parse(new Date())
    let user_title = this.data.user_title
    let user_url = this.data.user_url
    let index = this.data.index
    let target=this.data.target
    const _ = db.command
    console.log(index)
    db.collection('square').doc(_id).update({
      data: {
      message:_.push({
        each:[{

          user_url: user_url,
          user_title: user_title,
          message:message,
          time: newDateTime,
          _id: _id,
          type: 1,
          target:target
        }],
        position:Number(index)+1
      })
       
         

      
      }
    });

    this.setData({
      show: false,
      message: ''
    });


    setTimeout(() => {
      //延迟后进行的操作
      this.onLoad();

    }, 2000) //delayTime是延迟时间，以毫秒为单位，1000ms=1s

  },

//删除评论
del(e){
  console.log(e)
  wx.showLoading({
    title: '正在删除',
    mask:"true"
  })
  db.collection("square")
  .doc(e.target.id)
  .remove()
  .then(res=>{
    console.log(res)
    wx.hideLoading()
    setTimeout(() => {
      //延迟后进行的操作
      this.onLoad();

    }, 2000) //delayTime是延迟时间，以毫秒为单位，1000ms=1s

  })  
},


 //画廊结束
 imgList:function(e){
  // console.log(e.currentTarget.dataset.itemid);
  //执行放大过程
  this.setData({
    show: true,
    current:e.currentTarget.dataset.itemid
  })
 },


//图片点击事件
	//画廊
	img_view: function (e) {
		this.setData({
			img_view: true,
			imgUrls: [],
			img_view_index: ''
		})
		// console.log('dianjibig',e)
		var arr = e.currentTarget.id.split(",");
		// console.log('显示的照片',arr)
		this.setData({
			img_view: true,
			imgUrls: arr,
			img_view_index: e.target.id
		})
	},
})